package com.fates.common.codec;

import java.nio.charset.StandardCharsets;
import java.util.UUID;


public class SimpleCryptUtils {
	private static final String KEY_TAIL = "j@";

	public static String encrypt(String key, String data) {
		key = Md5Utils.md5Short(key) + KEY_TAIL;
		String encode = DesUtils.encode(key, data);
		byte[] bytes = Base64Utils.encodeToByte(encode.getBytes(StandardCharsets.UTF_8), true);
		return new String(bytes, StandardCharsets.UTF_8);
	}

	public static String decrypt(String key, String data) {
		key = Md5Utils.md5Short(key) + KEY_TAIL;
		byte[] bytes = Base64Utils.decode(data);
		data = new String(bytes, StandardCharsets.UTF_8);
		return DesUtils.decode(key, data);
	}

	public static void main(String[] args) {
		long s = System.currentTimeMillis();
		for (int i = 0; i < 2; i++) {
			String key = UUID.randomUUID().toString().replaceAll("-", "");
			String data = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
			String encode = encrypt(key, data);
			if (!data.equals(decrypt(key, encode))) {
				System.out.println(encode);
			}

		}

		System.out.println(System.currentTimeMillis() - s);
	}
}
